package ui;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

import util.ImagePanel;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;

import ui.backButton;

public class MenuUI implements ActionListener
{

	private static MenuUI instance = null;

	private MainWindow frameMenuUI;
	
	private backButton back;

	private JButton projects;
	private JButton offers;
	private JButton requests;

	private JButton materials;

	private JButton employees;
	private JButton customers;
	private JButton suppliers;
	private JButton subContractors;

	private JButton statistics;

	private JButton countries;
	private JButton cities;

	private JLabel items;
	private JLabel people;
	private JLabel locations;

	private Image image = null;
	private ImagePanel content;

	private AbstractButton logout;
	

	public void display()
	{
		frameMenuUI.setSize(700,400);
		frameMenuUI.setVisible(true);
		frameMenuUI.setResizable(false);
		frameMenuUI.setLocationRelativeTo(null);
	}

	public MenuUI()
	{		
		frameMenuUI = new MainWindow();
		back = new backButton();
		back.setNumber("1");
		

		items = new JLabel("ITEMS");
		items.setForeground(Color.white);
		items.setFont(new Font("Dialog", Font.BOLD, 20));

		people = new JLabel("PEOPLE");
		people.setForeground(Color.white);
		people.setFont(new Font("Dialog", Font.BOLD, 20));

		projects = new JButton();
		projects.setFont(new Font("Tahoma", Font.PLAIN, 15));
		projects.setText("PROJECTS");
		projects.setOpaque(false);
		projects.addActionListener(this);

		offers = new JButton();
		offers.setFont(new Font("Tahoma", Font.PLAIN, 15));
		offers.setText("OFFERS");
		offers.setOpaque(false);
		offers.addActionListener(this);

		requests = new JButton();
		requests.setFont(new Font("Tahoma", Font.PLAIN, 15));
		requests.setText("REQUESTS");
		requests.setOpaque(false);
		requests.addActionListener(this);

		materials = new JButton();
		materials.setFont(new Font("Tahoma", Font.PLAIN, 15));
		materials.setText("MATERIALS");
		materials.setOpaque(false);
		materials.addActionListener(this);

		employees = new JButton();
		employees.setFont(new Font("Tahoma", Font.PLAIN, 15));
		employees.setText("EMPLOYEES");
		employees.setOpaque(false);
		employees.addActionListener(this);

		customers = new JButton();
		customers.setFont(new Font("Tahoma", Font.PLAIN, 15));
		customers.setText("CUSTOMERS");
		customers.setOpaque(false);
		customers.addActionListener(this);

		suppliers = new JButton();
		suppliers.setFont(new Font("Tahoma", Font.PLAIN, 15));
		suppliers.setText("SUPPLIERS");
		suppliers.setOpaque(false);
		suppliers.addActionListener(this);

		subContractors = new JButton();
		subContractors.setFont(new Font("Tahoma", Font.PLAIN, 15));
		subContractors.setText("SUBCONTRACTOR");
		subContractors.setOpaque(false);
		subContractors.addActionListener(this);

		statistics = new JButton();
		statistics.setOpaque(false);
		statistics.setText("Statistics");
		statistics.addActionListener(this);

		countries = new JButton();
		countries.setFont(new Font("Tahoma", Font.PLAIN, 15));
		countries.setText("COUNTRIES");
		countries.setOpaque(false);
		countries.addActionListener(this);

		cities = new JButton();
		cities.setFont(new Font("Tahoma", Font.PLAIN, 15));
		cities.setText("CITIES");
		cities.setOpaque(false);
		cities.addActionListener(this);

		locations = new JLabel("LOCATIONS");
		locations.setForeground(Color.WHITE);
		locations.setFont(new Font("Dialog", Font.BOLD, 20));
		
		

		try
		{
			image = ImageIO.read(new File("img/background/background.jpg"));
		}
		catch(Exception e)
		{
			System.out.println("[ERROR] Cannot load background : " + e);
			e.printStackTrace();
		}

		content = new ImagePanel(image);
		
		logout = new JButton("Log out");
		logout.addActionListener(this);
		logout.setOpaque(false);
		GroupLayout groupLayout = new GroupLayout(frameMenuUI.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(10)
					.addComponent(people, GroupLayout.PREFERRED_SIZE, 133, GroupLayout.PREFERRED_SIZE)
					.addGap(89)
					.addComponent(items, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)
					.addGap(158)
					.addComponent(locations))
				.addComponent(content, GroupLayout.PREFERRED_SIZE, 694, GroupLayout.PREFERRED_SIZE)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(10)
					.addComponent(logout, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
					.addGap(474)
					.addComponent(statistics, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(people, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
						.addComponent(items, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
						.addComponent(locations, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE))
					.addGap(3)
					.addComponent(content, GroupLayout.PREFERRED_SIZE, 289, GroupLayout.PREFERRED_SIZE)
					.addGap(6)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(logout, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
						.addComponent(statistics, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)))
		);
		GroupLayout gl_content = new GroupLayout(content);
		gl_content.setHorizontalGroup(
			gl_content.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_content.createSequentialGroup()
					.addGap(10)
					.addGroup(gl_content.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_content.createSequentialGroup()
							.addComponent(employees, GroupLayout.PREFERRED_SIZE, 215, GroupLayout.PREFERRED_SIZE)
							.addGap(10)
							.addComponent(projects, GroupLayout.PREFERRED_SIZE, 215, GroupLayout.PREFERRED_SIZE)
							.addGap(10)
							.addComponent(countries, GroupLayout.PREFERRED_SIZE, 215, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_content.createSequentialGroup()
							.addComponent(customers, GroupLayout.PREFERRED_SIZE, 215, GroupLayout.PREFERRED_SIZE)
							.addGap(10)
							.addComponent(offers, GroupLayout.PREFERRED_SIZE, 215, GroupLayout.PREFERRED_SIZE)
							.addGap(10)
							.addComponent(cities, GroupLayout.PREFERRED_SIZE, 215, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_content.createSequentialGroup()
							.addComponent(suppliers, GroupLayout.PREFERRED_SIZE, 215, GroupLayout.PREFERRED_SIZE)
							.addGap(10)
							.addComponent(requests, GroupLayout.PREFERRED_SIZE, 215, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_content.createSequentialGroup()
							.addComponent(subContractors, GroupLayout.PREFERRED_SIZE, 215, GroupLayout.PREFERRED_SIZE)
							.addGap(10)
							.addComponent(materials, GroupLayout.PREFERRED_SIZE, 215, GroupLayout.PREFERRED_SIZE))))
		);
		gl_content.setVerticalGroup(
			gl_content.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_content.createSequentialGroup()
					.addGap(31)
					.addGroup(gl_content.createParallelGroup(Alignment.LEADING)
						.addComponent(employees, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
						.addComponent(projects, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
						.addComponent(countries, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE))
					.addGroup(gl_content.createParallelGroup(Alignment.LEADING)
						.addComponent(customers, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
						.addComponent(offers, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
						.addComponent(cities, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE))
					.addGroup(gl_content.createParallelGroup(Alignment.LEADING)
						.addComponent(suppliers, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
						.addComponent(requests, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE))
					.addGroup(gl_content.createParallelGroup(Alignment.LEADING)
						.addComponent(subContractors, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
						.addComponent(materials, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)))
		);
		content.setLayout(gl_content);
		frameMenuUI.getContentPane().setLayout(groupLayout);
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == logout)
		{
			frameMenuUI.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
			LoginUI log = LoginUI.getInstance();
			log.display();
		}
		else if(e.getSource() == projects)
		{
			frameMenuUI.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
	
			ProjectUI proj = ProjectUI.getInstance();
			proj.display();
		}
		else if(e.getSource() == offers)
		{
			frameMenuUI.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

			OfferUI offer = OfferUI.getInstance();
			offer.display();
		}
		else if(e.getSource() == requests)
		{
			frameMenuUI.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

			RequestsUI reqs = RequestsUI.getInstance();
			reqs.display();
		}
		else if(e.getSource() == materials)
		{
			frameMenuUI.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

			ItemUI it = ItemUI.getInstance();
			it.display();
		}
		else if(e.getSource() == employees)
		{
			frameMenuUI.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

			EmployeeUI emp = EmployeeUI.getInstance();
			emp.display();
		}
		else if(e.getSource() == customers)
		{
			frameMenuUI.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

			CustomerUI cus = CustomerUI.getInstance();
			cus.display();
		}
		else if(e.getSource() == suppliers)
		{
			frameMenuUI.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

			SupplierUI sup = SupplierUI.getInstance();
			sup.display();
		}
		else if(e.getSource() == subContractors)
		{
			frameMenuUI.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

			SubContractorUI sub = SubContractorUI.getInstance();
			sub.display();
		}
		else if(e.getSource() == cities)
		{
			frameMenuUI.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
			CityUI city = CityUI.getInstance();
			city.display();
		}
		else if(e.getSource() == countries)
		{
			frameMenuUI.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
			CountryUI ct = CountryUI.getInstance();
			ct.display();
		}
		else if(e.getSource() == statistics)
			{
				frameMenuUI.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
				
				StatisticsUI stats = StatisticsUI.getInstance();
				stats.display();
			}
		frameMenuUI.setVisible(false);
	}

	public static MenuUI getInstance()
	{
		if(instance == null)
			instance = new MenuUI();

		return instance;
	}

	public static void main(String[] args)
	{
		try 
		{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 

			MenuUI menu = new MenuUI();
			menu.display();
		}
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "Menu could not load", "Error", JOptionPane.ERROR_MESSAGE);
		}
	}
}